package com.ucode.tool.disruptor;

import java.util.concurrent.Executor;

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * Disruptor消费者之消息事件处理类
 * 单消费者：实现EventHandler接口(MessageEvent event, long sequence, boolean endOfBatch)方法<br>
 * 多消费者：实现WorkHandler接口的onEvent(MessageEvent event)方法<br>
 * --广播：对于多个消费者，每条信息会达到所有的消费者，被多次处理，一般每个消费者业务逻辑不通，用于同一个消息的不同业务逻辑处理<br>
 * --消费者之间无依赖关系 disruptor.handleEventsWith(handler1,handler2,handler3);<br>
 * --假设handler3必须在handler1，handler2处理完成后进行处理:disruptor.handleEventsWith(handler1,handler2).then(handler3);<br>
 * --分组：对于同一组内的多个消费者，每条信息只会被组内一个消费者处理，每个消费者业务逻辑一般相同，用于多消费者并发处理一组消息<br>
 * --假设handler1，handler2，handler3都实现了WorkHandler，则调用以下代码就可以实现分组：<br>
 * --disruptor.handleEventsWithWorkerPool(handler1, handler2, handler3);<br>
 * 广播和分组之间也是可以排列组合的<br>
 * @author: liliang
 * @date: 2020年3月1日 下午12:14:34
 */
public class DisruptorConsumer implements EventHandler<MessageEvent>, WorkHandler<MessageEvent>{

    private static Log log = LogFactory.get();
    
    private DisruptorHandleService disruptorService;
    
    private final Executor executor;
    
    public DisruptorConsumer(DisruptorHandleService disruptorService, Executor executor){
        this.disruptorService = disruptorService;
        this.executor = executor;
    }
    
    @Override
    public void onEvent(MessageEvent event) throws Exception {
        if (log.isDebugEnabled()){
            log.info("接受到数据更新请求  >>>" + event);
        }
        executor.execute(() ->{
            disruptorService.execute(event.getData());
        });
    }

    @Override
    public void onEvent(MessageEvent event, long sequence, boolean endOfBatch) throws Exception {
        if (log.isDebugEnabled())
        {
            log.info("接受到数据更新请求  >>>{}", event);
            log.info("Sequence:{}", sequence);
            log.info("End Of Batch：{}", endOfBatch);
        }
        this.onEvent(event);
        
    }

}
